package utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import file.File;


public class Utils {

	public static int[] parseIpString(String ip){
		int[] ipt = new int[4];
		/* Vérifier la validité de la string IP */
		String[] str = ip.split(".");
		for(int i = 0; i < str.length; i++){
			ipt[i] = Integer.parseInt(str[i]); 
		}
		return ipt;
	}

	public static int parsePortString(String port){
		/* Vérifier la valeur du port */
		return Integer.parseInt(port);
	}

	public static String seedListToString(ArrayList<File> files){
		String str = "";
		int i = 1;
		if (files!=null){
			for(File f : files){
				String space = (i < files.size()) ? " " : "";
				str += f.getName() + " " + f.getSize() + " " + f.getPieceSize() + " " + f.getIdFile() + space;
				i++;
			}
		}
		return str;
	}

	public static String leechListToString(ArrayList<File> files){
		String str = "";
		int i = 1;
		if (files!=null){
			for(File f : files){
				String space = (i < files.size()) ? " " : "";
				str += f.getIdFile() + space;
				i++;
			}
		}
		return str;
	}

	public static String getAllKeysFromFiles(ArrayList<File> files){
		String message = "";
		if(files != null){
			if (files.size()!=0)
			{
				message += files.get(0).getIdFile();
			}
			for (int i = 1; i<files.size(); i++){
				message += " "+files.get(i).getIdFile();
			}
		}
		return message; 
	}

	public static String byteArrayToString(byte[] b, int size){
		String str = "";
		for(int i = 0; i < size - 1; i++){
			str += (char) b[i];
		}
		return str;
	}

	public static byte[] binaryStringToByte(String s){
		byte[] b = new byte[s.length()];
		for(int j = 0; j<s.length(); j++){
			if(s.charAt(j) == '0')
				b[j] = 0;
			else if(s.charAt(j) == '1')
				b[j] = 1;
			else return null;
		}
		return b;
	}

	public static String byteArrayListToString(ArrayList<Byte> b){
		String str = "";
		for(int i = 0; i < b.size() - 1; i++){
			str += b.get(i);
		}
		return str;
	}
	
	//TODO Message Framing
	public static String readStringStream(InputStream in){
		int c;
		BufferedInputStream bis = new BufferedInputStream(in);
		InputStreamReader isr = new InputStreamReader(bis);
		StringBuffer strbuf = new StringBuffer();
		try {
			c = isr.read();
			if(c == -1){
				return null;
			}
			while(c != -1){
				strbuf.append((char) c);
				c = isr.read();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(strbuf.length() > 0){
			if(strbuf.charAt(strbuf.length() - 1) == '\0'){
				return new String(strbuf).substring(0, strbuf.length() - 1);
			}
			else{
				return new String(strbuf);
			}
		}
		else{
			return null;
		}
	}

	public static byte[] readByteStream(InputStream in){
		String data = readStringStream(in);
		return data.getBytes();
	}

	public static File getFileFromArrayList(ArrayList<File> list, String key){
		for(int i = 0; i<list.size();i++){
			if(list.get(i).getIdFile().equals(key)){
				return list.get(i);
			}
		}
		return null;
	}
}

